package com.authine.cloudpivot.ext.dto.model.trade;

import com.authine.cloudpivot.ext.Enum.TradeDirection;
import com.authine.cloudpivot.ext.Enum.TradeOrderGoodsStatus;
import com.authine.cloudpivot.ext.dto.Summary;
import com.authine.cloudpivot.ext.enums.order.OrderGoodsStatusEnum;
import com.authine.mvp.app.launcher.domain.SelectorFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "通过订单生成交易实体类")
public class OrderGoodsConvertTrade {

    private String erpPo;
    private String sku;
    /**
     * 订单类型(普通、代购)，枚举类 TradeOrderType
     */
    private String businessType;
    @Schema(description = "客户组织")
    private SelectorFormat customerOrganizationId;
    @Schema(description = "供应商组织")
    private SelectorFormat vendorOrganizationId;
    @Schema(description = "下单用户ID")
    private String memberId;
    @Schema(description = "下单用户名")
    private String memberName;
    @Schema(description = "供应商订单商品")
    private String vendorOrderGoodsId;
    @Schema(description = "客户订单商品")
    private String customerOrderGoodsId;
    @Schema(description = "商品含税单价")
    private BigDecimal price;
    @Schema(description = "商品数量")
    private BigDecimal quantity;
    @Schema(description = "商品税率")
    private BigDecimal taxRate;
    @Schema(description = "商品金额，等于数量X含税单价")
    private BigDecimal total;
    @Schema(description = "账期")
    private BigDecimal accountPeriod;
    @Schema(description = "商品概要")
    private Summary summary;
    @Schema(description = "供应商订单ID")
    private String vendorOrderId;
    @Schema(description = "客户订单ID")
    private String customerOrderId;
    @Schema(description = "运费")
    private BigDecimal freight;
    @Schema(description = "金税编码")
    private String goldenTax;
    @Schema(description = "订单商品状态")
    private String orderGoodsStatus;
    @Schema(description = "总金额(包含运费)")
    private BigDecimal amount;
    @Schema(description = "是否转采")
    private boolean transferPurchase;
    @Schema(description = "订单备注")
    private String description;
    @Schema(description = "拓展字段")
    private TradeExtendsAttr extendsAttr;
    @Schema(description = "erppo行号")
    private String erpPoLineNumber;
    @Schema(description = "询价时间")
    private String inquiryTime;

    @Schema(description = "未税单价")
    private BigDecimal pretax_price;
    @Schema(description = "商品未税金额（不包含运费）")
    private BigDecimal total_before_tax;
    @Schema(description = "总未税金额(包含运费)")
    private BigDecimal amount_before_tax;

    @Schema(description = "订单收货人")
    private String order_receiver;
    @Schema(description = "订单收货地址")
    private String order_address;


    /**     以下字段交易创建时赋值    **/
    private String actualAccountPeriod;


    public Trade convertToSaleTrade(){
        return Trade.builder()
                .erp_po(this.getErpPo())
                .sku(this.getSku())
                .direction(TradeDirection.Sale.name())
                .business_type(this.getBusinessType())
                .organization_id(this.getCustomerOrganizationId())
                .organization_name(this.getCustomerOrganizationId().getName())
                .target_organization_name(this.getVendorOrganizationId().getName())
                .member_id(this.getMemberId())
                .member_name(this.getMemberName())
                .order_goods_id(this.getCustomerOrderGoodsId())
                .target_order_goods_id(this.getVendorOrderGoodsId())
                .price(this.getPrice())
                .quantity(this.getQuantity())
                .tax_rate(this.getTaxRate())
                .total(this.getTotal())
                .pretax_price(this.getPretax_price())
                .total_before_tax(this.getTotal_before_tax())
                .amount_before_tax(this.getAmount_before_tax())
                .account_period(this.getAccountPeriod())
                .summary(this.getSummary())
                .unit(this.summary.getUnit())
                .vendor_order_id(this.getVendorOrderId())
                .customer_order_id(this.getCustomerOrderId())
                .freight(this.getFreight())
                .golden_tax(this.getGoldenTax())
                .order_goods_status(OrderGoodsStatusEnum.valueOf(this.getOrderGoodsStatus()))
                .amount(this.getAmount())
                .transfer_purchase(this.transferPurchase)
                .memo(this.getDescription())
                .extends_attr(this.getExtendsAttr().setInquiryTime(this.getInquiryTime()))
                .clientFactoryCode(this.getExtendsAttr().getClientFactoryCode())
                .erp_po_line_number(this.getErpPoLineNumber())
                .order_address(this.getOrder_address())
                .order_receiver(this.getOrder_receiver())
                .allowed_invoice_quantity(BigDecimal.ZERO)
                .allowed_invoice_total(BigDecimal.ZERO)
                .allowed_settle_amount(BigDecimal.ZERO)
                .settle_status("Undo")
                .invoice_status("Undo")
                .build();
    }

    public Trade convertToPurchaseTrade(){
        return Trade.builder()
                .erp_po(this.getErpPo())
                .sku(this.getSku())
                .direction(TradeDirection.Purchase.name())
                .business_type(this.getBusinessType())
                .organization_id(this.getVendorOrganizationId())
                .organization_name(this.getVendorOrganizationId().getName())
                .target_organization_name(this.getCustomerOrganizationId().getName())
                .member_id(this.getMemberId())
                .member_name(this.getMemberName())
                .order_goods_id(this.getVendorOrderGoodsId())
                .target_order_goods_id(this.getCustomerOrderGoodsId())
                .price(this.getPrice())
                .quantity(this.getQuantity())
                .tax_rate(this.getTaxRate())
                .total(this.getTotal())
                .pretax_price(this.getPretax_price())
                .total_before_tax(this.getTotal_before_tax())
                .amount_before_tax(this.getAmount_before_tax())
                .account_period(this.getAccountPeriod())
                .summary(this.getSummary())
                .unit(this.summary.getUnit())
                .vendor_order_id(this.getVendorOrderId())
                .customer_order_id(this.getCustomerOrderId())
                .freight(this.getFreight())
                .golden_tax(this.getGoldenTax())
                .order_goods_status(OrderGoodsStatusEnum.valueOf(this.getOrderGoodsStatus()))
                .amount(this.getAmount())
                .transfer_purchase(this.transferPurchase)
                .memo(this.getDescription())
                .extends_attr(this.getExtendsAttr().setInquiryTime(this.getInquiryTime()))
                .clientFactoryCode(this.getExtendsAttr().getClientFactoryCode())
                .erp_po_line_number(this.getErpPoLineNumber())
                .order_address(this.getOrder_address())
                .order_receiver(this.getOrder_receiver())
                .allowed_invoice_quantity(BigDecimal.ZERO)
                .allowed_invoice_total(BigDecimal.ZERO)
                .allowed_settle_amount(BigDecimal.ZERO)
                .settle_status("Undo")
                .invoice_status("Undo")
                .build();
    }


}
